package com.universal_library.fragment;

import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.universal_library.R;
import com.universal_library.adapter.BaseAdapter;
import com.universal_library.weight.DefineLoadMoreView;
import com.universal_library.weight.WrapContentLinearLayoutManager;
import com.yanzhenjie.recyclerview.swipe.SwipeItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

/**
 * Created by solin on 2018/1/29.
 */

public abstract class BaseNoRefreshRecycleViewFragment<T> extends BaseFragment implements SwipeItemClickListener, SwipeMenuRecyclerView.LoadMoreListener, View.OnClickListener {
    protected SwipeMenuRecyclerView my_recycleView;
    protected SwipeMenuRecyclerView.LayoutManager mLayoutManager;
    protected RecyclerView.ItemDecoration mItemDecoration;


    protected BaseAdapter adapter = null;
    protected View view_nodata, view_loading;

    protected int pageSize = 20;// 条数
    protected int pageNo = 1;//页码

    @Override
    protected int getLayoutId() {
        return R.layout.base_norefreshfragment_recycleview;
    }

    @Override
    protected void initView(View view) {

        my_recycleView = (SwipeMenuRecyclerView) view.findViewById(R.id.my_recycleView);
        LinearLayoutManager linearLayoutManager = new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

        mItemDecoration = new DefaultItemDecoration(ContextCompat.getColor(getActivity(), R.color.main_background));

        my_recycleView.setLayoutManager(linearLayoutManager);
        my_recycleView.addItemDecoration(mItemDecoration);
        my_recycleView.setSwipeItemClickListener(this);


        DefineLoadMoreView loadMoreView = new DefineLoadMoreView(getContext());
        my_recycleView.addFooterView(loadMoreView); // 添加为Footer。
        my_recycleView.setLoadMoreView(loadMoreView); // 设置LoadMoreView更新监听。
        my_recycleView.setLoadMoreListener(this);
        adapter = getRecycleAdapter();
        my_recycleView.setAdapter(adapter);
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                if (my_recycleView != null && my_recycleView.getAdapter() == null) {
                    my_recycleView.setAdapter(adapter);
                }
            }
        });
        view_nodata = view.findViewById(R.id.vs_nodata);
        view_loading = view.findViewById(R.id.view_loading);
        view_nodata.setOnClickListener(this);
    }

    /**
     * 数据集合
     *
     * @param listDatas
     * @param total     一个多少调数据  是否需要调用加载更多
     */
    public void setmData(List<T> listDatas, int total) {
        if (pageNo == 1)
            //第一次加载  或者刷新
            myRefresh(listDatas, total);
        else
            //加载更多
            myLoadMore(listDatas);
    }

    /**
     * 加载刷新  使用
     *
     * @param listDatas
     * @param total
     */
    private void myRefresh(List<T> listDatas, int total) {
        if (listDatas == null) {
            //加载错误页
            view_loading.setVisibility(View.GONE);
            view_nodata.setVisibility(View.VISIBLE);
            my_recycleView.setVisibility(View.GONE);
        } else {
            //加载recycleview
            if (listDatas.size() == 0) {
                view_loading.setVisibility(View.GONE);
                view_nodata.setVisibility(View.VISIBLE);
                my_recycleView.setVisibility(View.GONE);
                //加载无数据页面
            } else {
                //正常显示数据
                view_loading.setVisibility(View.GONE);
                view_nodata.setVisibility(View.GONE);
                my_recycleView.setVisibility(View.VISIBLE);
                if (my_recycleView.getAdapter() == null) {
                    adapter = getRecycleAdapter();
                    my_recycleView.setAdapter(adapter);
                }

                adapter.setDatas(listDatas);
                // 第一次加载数据：一定要掉用这个方法。
                // 第一个参数：表示此次数据是否为空，
                // 第二个参数：表示是否还有更多数据
                if (total == listDatas.size())
                    my_recycleView.loadMoreFinish(false, false);
                else
                    my_recycleView.loadMoreFinish(false, true);
            }
        }
    }

    /**
     * 加载更多使用
     *
     * @param listDatas
     */
    private void myLoadMore(List<T> listDatas) {
        if (listDatas == null) {
            //可重新加载更多
            my_recycleView.loadMoreError(0, "请求网络失败");
            my_recycleView.loadMoreFinish(false, true);
        } else if (listDatas.size() == 0) {
            my_recycleView.loadMoreFinish(false, false);
        } else {
            adapter.addDatas(listDatas);
            my_recycleView.loadMoreFinish(false, true);
        }
    }

    //返回adapter
    protected abstract BaseAdapter getRecycleAdapter();


    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        pageNo++;
//        initData();
        // 如果加载失败调用下面的方法，传入errorCode和errorMessage。
        // errorMessage是会显示到loadMoreView上的，用户可以看到。
        // mRecyclerView.loadMoreError(0, "请求网络失败");
        my_recycleView.loadMoreFinish(false, false);

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.vs_nodata) {
            pageNo = 1;
            initData();
        }
    }
}

